package com.ruoyi.system.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.GaUser;
import com.ruoyi.system.domain.GaUserExam;
import com.ruoyi.system.dto.input.UserExamSubjectGetInput;
import com.ruoyi.system.dto.input.UserPasswordUpdateInput;
import com.ruoyi.system.dto.input.UserScoreUpdateInput;
import com.ruoyi.system.dto.input.UserSubjectConditionGetInput;
import com.ruoyi.system.enservice.IUserEnService;
import com.ruoyi.system.service.IGaUserExamService;
import com.ruoyi.system.service.IGaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/front/user")
public class UserController extends BaseController {

    @Autowired
    private IGaUserService gaUserService;

    @Autowired
    private IUserEnService userEnService;

    @Autowired
    private IGaUserExamService userExamService;

    /**
     * 更新学生登陆密码
     * @author xuankai
     * @param dto 学生id、学生联系电话、学生新密码
     * @return 是否更新成功
     **/
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(UserPasswordUpdateInput dto){
        GaUser user = new GaUser();
        user.setId(dto.getStudentId());
        user.setTel(dto.getStudentTel());
        user.setUserPassword(dto.getStudentPassword());
        if(gaUserService.updateGaUser(user) == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    /**
     * 更新学生的成绩
     * @author xuankai
     * @param dto 学生id、考试编号、考试成绩
     * @return 是否更新成功
     **/
    @PostMapping("/updateScore")
    public AjaxResult updateScore(UserScoreUpdateInput dto){
        GaUserExam userExam = new GaUserExam();
        userExam.setUser(dto.getStudentId());
        userExam.setExamPaper(dto.getExamId());
        userExam.setScore(dto.getScore());
        if(userExamService.updateGaUserExam(userExam) == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    /**
     * 通过学生ID查询学生考试信息和是否完成答题(IF-05)
     * @param student 学生ID
     * @return 学生信息
     */
    @GetMapping("/examInfo/{student}")
    public AjaxResult getExamInfo(@PathVariable("student") Long student){
        return AjaxResult.success(userEnService.getExamInfo(student));
    }

    /**
     * 根据学生id和试卷id查询学生参与考试的题目
     * @param dto
     * @return
     */

    @GetMapping("/examSubject")
    public AjaxResult getExamSubject(UserExamSubjectGetInput dto){
        return AjaxResult.success(userEnService.getExamSubject(dto));
    }

    /**
     * 根据学生id和试卷id查询学生考试题目对错
     * @param dto
     * @return
     */

    @GetMapping("/subjectCondition")
    public AjaxResult getSubjectCondition(UserSubjectConditionGetInput dto){
        return AjaxResult.success(userEnService.getSubjectCondition(dto));
    }

    /**
     * 根据学生id查询学生的相关信息
     * @param studentId
     * @return
     */

    @GetMapping("/information/{studentId}")
    public AjaxResult getInformation(Long studentId){
        return AjaxResult.success(userEnService.getInformation(studentId));
    }

    /**
     * @Description: 用户登陆接口
     * @author ckyforever
     * @param userId 用户Id
     * @param password 用户密码
     * @return 是否登入成功
     **/
    @PostMapping("/login")
    public AjaxResult login(String userId,String password) {
        GaUser user = new GaUser();
        user.setUserId(userId);
        user.setUserPassword(password);
        List<GaUser> list = gaUserService.selectGaUserList(user);
        if(list.size() != 0) {
            return AjaxResult.success(list.get(0));
        } else {
            return AjaxResult.error();
        }
    }
}
